package org.electronic.registration.dao;

import org.electronic.registration.bo.Patient;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Egor
 * Date: 12.01.12
 */
@Service
public class PatientDaoImpl extends BaseDao implements PatientDao {
    @Override
    public void save(Patient patient) {
        getHibernateTemplate().saveOrUpdate(patient);
    }

    @Override
    public Patient getPatientByName(String name) {
        return (Patient) getHibernateTemplate().findByNamedQuery(Patient.GET_PATIENT_BY_NAME, name).get(0);

    }

    @Override
    public List<Patient> getAllPatients() {
        return (List<Patient>) getHibernateTemplate().findByNamedQuery(Patient.GET_ALL_PATIENTS);
    }

    @Override
    public Patient isPatientExist(String email, String password) {
        Patient patient = (Patient) getHibernateTemplate().findByNamedQuery(Patient.GET_PATIENT_BY_EMAIL, email).get(0);
        if (patient != null && password.equals(patient.getPassword())) {
            return patient;
        }
        return null;
    }
}
